package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.comment.repository.StrategyCommentPageRepository;
import cn.wolfcode.luowowo.comment.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.AllArgsConstructor;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private StrategyCommentPageRepository strategyCommentPageRepository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void commentAdd(StrategyComment strategyComment) {

        strategyComment.setCreateTime(new Date());

        //将评论保存到Mongodb中去
        strategyCommentRepository.insert(strategyComment);

    }

    @Override
    public Page queryPageForComment(StrategyCommentQueryObject qo) {

//        //两个重要sql  一个分页条件  一个PageImpl 实现类接受
//        Query query = new Query();
//
//        //设置查询条件   静态方法
////        query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
//        Criteria criteria =  Criteria.where("detailId").is(qo.getDetailId());;
//        query.addCriteria(criteria);
//
//        //查询满足条件的总数
//        long count = template.count(query, StrategyComment.class);
//
//        if (count == 0) {
//
//            //返回空
//            return Page.empty();
//        }
//
//
//        //pageable添加分页条件
//        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), new Sort(Sort.Direction.DESC, "createTime"));
//
//        //添加分页条件
//        query.with(pageable);
//
//        //查询每页数据
//        List<StrategyComment> strategyComments = template.find(query, StrategyComment.class);

        Page<StrategyComment> page = strategyCommentPageRepository.findByDetailId(qo.getDetailId(), PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), new Sort(Sort.Direction.DESC, "createTime")));

//        return new PageImpl(strategyComments, pageable, count);

        return page;





    }

    @Override
    public StrategyComment strategyThumbup(String toid, Long fromid) {

        //先查出这个对象
        if (StringUtils.hasLength(toid) && fromid != null) {
            StrategyComment strategyComment = strategyCommentRepository.findById(toid).get();

            List<Long> thumbuplist = strategyComment.getThumbuplist();

            int thumbupnum = strategyComment.getThumbupnum();

            //包含,就是取消
            if (thumbuplist.contains(fromid)) {

                thumbuplist.remove(fromid);

                thumbupnum = thumbupnum - 1;

            } else {

                thumbuplist.add(fromid);

                thumbupnum = thumbupnum + 1;

            }

            strategyComment.setThumbupnum(thumbupnum);

            strategyCommentRepository.save(strategyComment);

            return strategyComment;

        }

        return null;
    }

}
